/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.coverage_measuring.mutator;

import moved.org.objectweb.asm.Label;
import picounit.features.classloader.mutator.MethodMutator;

public class CoverageMethodMutator extends MethodMutator {
	private static final String CLASS_DESCRIPTION =
		getDescriptionOf(CoverageMethodMutator.class);

	public static final AggregateCoverageListener coverageListener =
		new AggregateCoverageListener();

	private final IdSource idSource;

	public CoverageMethodMutator(CoverageListener coverageListener, IdSource idSource) {
		CoverageMethodMutator.coverageListener.add(coverageListener);

		this.idSource = idSource;
	}

	@Override
	public void visitLineNumber(int lineNumber, Label start) {
		super.visitLineNumber(lineNumber, start);

		super.visitLdcInsn(idSource.nextId(method().className(), method().methodName(), lineNumber));
		super.visitMethodInsn(INVOKESTATIC, CLASS_DESCRIPTION, "covered", "(I)V");
	}

	public static void covered(int coverageId) {
		coverageListener.covered(coverageId);
	}

	///////////////////////////////////////////////////////////////////////////////////////////
	// Implementation Methods
	///////////////////////////////////////////////////////////////////////////////////////////

	private static String getDescriptionOf(Class aClass) {
		return aClass.getName().replace('.', '/');
	}
}

